/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.uhk.ppro.traveler.logic;

import cz.uhk.ppro.traveler.dao.TransactionsDao;
import cz.uhk.ppro.traveler.dao.UsersDao;
import cz.uhk.ppro.traveler.entity.TTransaction;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 *
 * @author Martin Soušek
 */
public class TransactionActionBean extends BaseActionBean{
    
    private TTransaction transaction;
    
    @SpringBean("users")
    private UsersDao usersDao;
    
    @SpringBean("transactions")
    private TransactionsDao transactionsDao;

    public TTransaction getTransaction() {
        return transaction;
    }

    public void setTransaction(TTransaction transaction) {
        this.transaction = transaction;
    }
    
    @DefaultHandler
    public Resolution addTransaction(){
        transaction.settDate(new Date());
        getContext().getCurrentUser().getBudget().getTransactions().add(transaction);
        transaction.setBudget(getContext().getCurrentUser().getBudget());
        getContext().getCurrentUser().getBudget().calculateTotal();
        usersDao.addUser(getContext().getCurrentUser());
        return new ForwardResolution("/WEB-INF/jsp/home.jsp");
    }
    
    public Resolution deleteTransaction(){
        TTransaction t = transactionsDao.getTransaction(Integer.parseInt(transaction.getId()+""));
        getContext().getCurrentUser().getBudget().getTransactions().remove(t);
        transactionsDao.removeTransaction(t);
        getContext().getCurrentUser().getBudget().calculateTotal();
        return new ForwardResolution("/WEB-INF/jsp/home.jsp");
    }
    
}
